package com.pda.xinmazhi.ui.login;


import android.speech.tts.TextToSpeech;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;

import com.pda.xinmazhi.R;
import com.pda.xinmazhi.contract.LoginContract;
import com.pda.xinmazhi.presenter.LoginPresenter;
import com.tencent.bugly.crashreport.CrashReport;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.config.BaseUrl;
import com.wangshen.base.net.client.KRetrofitConfig;
import com.wangshen.base.net.client.KRetrofitFactory;
import com.wangshen.base.net.interceptor.ExCookieInterceptor;
import com.wangshen.base.permission.AddPermission;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.base.util.SharedPreferencesUtil;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = AppActivityKey.LOGIN)
public class LoginActivity extends BaseMvpActivity<LoginPresenter> implements LoginContract.View {
    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_pw)
    EditText etPw;
    @BindView(R.id.et_ip)
    EditText etIp;
    @BindView(R.id.cb_pw)
    CheckBox cbPw;
    @BindView(R.id.bt_login)
    Button btLogin;
    private TextToSpeech tts;
    @Override
    public LoginPresenter getPresenter() {
        return new LoginPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        CrashReport.initCrashReport(getApplicationContext(), "d440a67057", false);
        AddPermission addPermission = new AddPermission(this);
        addPermission.addPermission((hasPermission, code) -> {
            if (!hasPermission){
                showMessage("请授于相关权限");
                finishActivity();
            }
        }, AddPermission.CODE_PERMISSIONS_LOCATION);
        boolean is = SharedPreferencesUtil.getInstance().getBoolean("isPassword", false);
        etPw.setText(SharedPreferencesUtil.getInstance().getString("password", ""));
        etUsername.setText(SharedPreferencesUtil.getInstance().getString("account", ""));
        etIp.setText(SharedPreferencesUtil.getInstance().getString("ip", ""));
        cbPw.setChecked(is);
     // initTTS();

    }
    private String digits = "0123456789.:";
    private boolean isIp(String ip){
        boolean isIp=false;
        for (int i=0;i< ip.length();i++){
            char c=ip.charAt(i);
            if (!digits.contains(String.valueOf(c))){
                isIp=true;
            }
        }
        return isIp;
    }
    @Override
    public void initLinsenterner() {

    }

    @Override
    public void getData() {

    }


    @OnClick(R.id.bt_login)
    public void onViewClicked() {
        //Intent intent = new Intent("com.summi.scan");

//注意：设备中含有ScannerHead组件v4.4.4及以上版本

        if (TextUtils.isEmpty(etUsername.getText())) {
            showMessage("用户名不能为空");
            return;
        }
        if (TextUtils.isEmpty(etPw.getText())) {
            showMessage("密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(etIp.getText())) {
            showMessage("IP地址不能为空");
            return;
        }
        if (isIp(etIp.getText().toString())){
            showMessage("IP地址格式错误");
            return;
        }
        BaseUrl.BASEURL = "http://" + etIp.getText().toString() + "/";
        KRetrofitConfig config = new KRetrofitConfig.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .retryOnConnectionFailure(false)
                .setConnectTimeout(20)
                .setReadTimeout(20)
                .setWriteTimeout(20)
                .addInterceptor(new ExCookieInterceptor())
                .build();
        KRetrofitFactory.init(config);
        SharedPreferencesUtil.getInstance().putBoolean("isPassword", cbPw.isChecked());
        ExCookieInterceptor.TOKEN="";
        presenter.signIn(etUsername.getText().toString(), etPw.getText().toString(), etIp.getText().toString());
    }


    @Override
    public void onSuccess() {
        SharedPreferencesUtil.getInstance().putBoolean("isPassword", cbPw.isChecked());
        if (cbPw.isChecked()){
            SharedPreferencesUtil.getInstance().putString("account",etUsername.getText().toString());
            SharedPreferencesUtil.getInstance().putString("password",etPw.getText().toString());
            SharedPreferencesUtil.getInstance().putString("ip",etIp.getText().toString());
        }else {
            SharedPreferencesUtil.getInstance().putString("account","");
            SharedPreferencesUtil.getInstance().putString("password","");
            SharedPreferencesUtil.getInstance().putString("ip","");
        }
    }
}
